import axios from "axios"
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import { isUserTempId, readToken } from "@/utils/auth"

import router from "@/router"
import { Message } from "element-ui";

// 创建实例对象
let instance = axios.create({
    baseURL: "/sph",
    timeout: 5000
})

// 请求拦截
instance.interceptors.request.use((config) => {
    NProgress.start()

    // 向请求头添加 token 用户登录后的真正的唯一标识
    if (readToken()) {
        config.headers.token = readToken();
    }

    //向请求头添加 usertempId 用户临时唯一标识
    config.headers.userTempId = isUserTempId();
    return config
})

// 响应拦截
instance.interceptors.response.use(
    (response) => {
        NProgress.done()

        switch (response.data.code) {
            case 200:
                return response.data.data
            case 201:
                return "响应拦截器中的提示:参数错误";
            case 208:  //未登录
                //跳转值登录页面
                router.push("/login");
                // 提示
                // Message.warning("请先登录!");
                return response.data.code;
            default:
                return response.data
        }
        return response.data
    },
    (error) => {
        console.log(error.message)
        NProgress.done()
        return new Promise(() => { })
    }
)




export default instance